#!F:\environment\python\python.exe
# author: fany
import time
import requests
from bs4 import BeautifulSoup
import re


def loading():
    num = 0
    print()
    for j in range(7):
        print("\r%s" % num, "%", '-------------------' * j,  end='', flush=True)
        num += 16
        if num > 80:num = 100
        time.sleep(0.05)
    print()


def main():
    query_word = []
    flag = True
    while flag:
        name = input('请输入词条:')
        while name == '':
            name = input('请输入词条:')
        query_word.append(name)
        headers = {
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:73.0) Gecko/20100101 Firefox/73.0",
        }
        xx = requests.session()
        xx = xx.get('https://baike.baidu.com/item/{}'.format(name), headers=headers)
        page = BeautifulSoup(xx.text, 'lxml')
        try:
            p1 = page.select('.lemma-summary')
            text = p1[0].text
            text = re.sub(r'\[.*?\]', '', text)
            text = text.replace('\n', '')
            text = text.replace(' ', '')
            text = text.split('。')

            p2 = page.select('.para')
            for i, j in enumerate(p2, start=0):
                p2[i] = p2[i].text
                p2[i] = re.sub(r'\[.*?\]', '', p2[i])
                p2[i] = p2[i].replace('\n', '')
                p2[i] = p2[i].replace(' ', '')
            p2[0] = ''
            text2 = ''.join(p2)
            text2 = text2.split('。')

            print(name, '的资料为：')
            loading()
            for j, i in enumerate(text, start=0):
                if i == '':
                    i = '完毕'
                print('  {}'.format(j+1), i)
            print()
            print('--' * 60)
            look_is = input('请输入是否查看详细信息：（输入look查看 输入回车跳过）|')
            if look_is == 'look':
                loading()
                for j, i in enumerate(text2, start=0):
                    if i == '':
                        i = '完毕'
                    print('  {}'.format(j+1), i)
                print()
                print('--' * 60)
        except:
            print('查无此词：')
            time.sleep(1)
        print('按回车继续 输入exit退出')
        flag_lin = input('请输入判断：')
        if flag_lin == 'exit':
            flag = False
    print('查询结束，按回车键后5秒自动关闭, 您一共查询了{}个词条：'.format(len(query_word)))
    for i, j in enumerate(query_word, start=1):
        print(j, end='  ')
        if i % 5 == 0:print()
    input()


if __name__ == '__main__':
    main()
    for i in range(5, 0, -1):
        print('\r{}秒后关闭'.format(i), end='', flush=True)
        time.sleep(1)
